package com.jisf.driver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jisf.driver.entity.po.ApplicationPackage;
import com.jisf.driver.entity.po.DrivingSchoolLog;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.mapper.ApplicationPackageMapper;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.IApplicationPackageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisf.driver.utils.LogUtil;
import com.jisf.driver.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-05-12
 */
@Service
public class ApplicationPackageServiceImpl extends ServiceImpl<ApplicationPackageMapper, ApplicationPackage> implements IApplicationPackageService {

    @Autowired
    private ApplicationPackageMapper applicationPackageMapper;

    @Autowired
    private LogUtil log;

    /**
     * 获取套餐列表
     * @return
     */
    @Override
    public List<ApplicationPackage> getApplyList() {
        try {
            List<ApplicationPackage> applicationPackages = applicationPackageMapper.selectList(null);
            return applicationPackages;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    @Override
    public ResultCode deleteApply(Integer id) {
        try {
            int delete = applicationPackageMapper.deleteById(id);

            // 存储日志
            String operator = UserUtil.usernameThreadLocal.get();
            String msg = String.format("用户名为%s的用户删除id为%d的套餐", operator, id);

            if (delete != 0){
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
                log.info(drivingSchoolLog);
                return ResultCode.APPLY_DELETE_SUCCESS;
            } else {
                String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
                log.info(drivingSchoolLog);
                return ResultCode.APPLY_DELETE_FAILED;
            }
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 添加套餐
     * @param applicationPackage
     * @return
     */
    @Override
    public ResultCode addApply(ApplicationPackage applicationPackage) {
        int insert = applicationPackageMapper.insert(applicationPackage);

        // 存储日志
        String operator = UserUtil.usernameThreadLocal.get();
        String msg = String.format("用户名为%s的用户增加id为%d的套餐", operator, applicationPackage.getId());

        if (insert != 0){
            String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, true));
            log.info(drivingSchoolLog);
            return ResultCode.APPLY_ADD_SUCCESS;
        }else {
            String drivingSchoolLog = JSONObject.toJSONString(new DrivingSchoolLog(operator, msg, false));
            log.info(drivingSchoolLog);
            return ResultCode.APPLY_ADD_FAILED;
        }
    }
}
